<?php

/**
 * 微信公众号工具类
 * @author Reer
 * @email reer_shu_re@163.com
 */
namespace app\common\utils;

use EasyWeChat\Factory;
use EasyWeChat\OfficialAccount\Application;

class PublicWechatUtil extends BaseUtil
{
    static $_self = null;

    /**@var Application $app*/
    private static $app;
    public static function getInstance() {
        if (empty(self::$_self)) {
            self::$_self = new PublicWechatUtil();
            $config = [
                'app_id' => config('project.PUBLIC_WECHAT.APPID'),
                'secret' => config('project.PUBLIC_WECHAT.APPSECRET'),
                'response_type' => 'array',
            ];
            self::$app = Factory::officialAccount($config);
        }
        return self::$_self;
    }

    /** 微信公众号授权 参数为授权完成最后要跳转的url存放于授权state额外参数中
     * @param $redirectUrl
     */
    public function oauth($redirectUrl){
        self::$app->oauth->scopes(['snsapi_userinfo'])
            ->setRedirectUrl(config('project.REDIRECTDOMAIN')."/api/OpenApi/wechatCallback/goUrl/".base64_encode($redirectUrl))
            ->redirect()->send();
    }

    /**
     * 获取授权回调的用户信息
     */
    public function getOauthInfo(){
        $user = self::$app->oauth->user();
        return $user;
    }
}